/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.invoice.controller;


import com.invoice.busines.ClienteBO;
import com.invoice.busines.ClienteBOImpl;
import com.invoice.busines.dto.ClienteDTO;
import com.invoice.busines.dto.ClienteCampoDTO;
import com.invoice.exceptions.InvoiceException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Lenin
 */
@ManagedBean
@SessionScoped
public class ClienteBean extends BeanGenerico{
    private Logger logger=Logger.getLogger(ClienteBean.class.getName());
    ClienteBO clienteBO=new ClienteBOImpl();
    private static final String NOREDIRECCIONAR = "ListaClientes";
    private static boolean ACTIVO=true;
    
    public ClienteBean(){
        
    }
    private ClienteDTO clienteDTO;
    private boolean habBusqueda;
    private boolean habControles;
    private boolean habEdicion;  
    private List<ClienteCampoDTO> labels = new ArrayList<ClienteCampoDTO>();

    @Override
    protected void init() {
        clienteDTO=new ClienteDTO();
        habControles=false;
         habEdicion=false;
    }

    public List<ClienteCampoDTO> getLabels() {
        return labels;
    }
    
    public void findCliente(){
        try {
            String codCli=clienteDTO.getCerCli();
            clienteDTO=clienteBO.findCliente(codCli);
            if(clienteDTO!=null){
                msg=new FacesMessage(FacesMessage.SEVERITY_INFO, "Cliente encontrado", "Cliente ya esta registrado");
                dispMessage();
            }else{
                clienteDTO=new ClienteDTO();
                clienteDTO.setCerCli(codCli);
                msg=new FacesMessage(FacesMessage.SEVERITY_WARN, "Cliente no encontrado", "Desea registrar un nuevo cliente?");
                habControles=true;
                dispMessage();
            }
        } catch (Exception ex) {
            Logger.getLogger(ClienteBean.class.getName()).log(Level.SEVERE, null, ex);
            msg=new FacesMessage(FacesMessage.SEVERITY_FATAL, "Error", ex.getMessage());
            dispMessage();
        }
    }
    public void cancelFound(){
        clienteDTO=new ClienteDTO();
        habBusqueda=true;
    }
    
    public String addCliente(){
        habEdicion = false;
        clienteDTO = new ClienteDTO();
        labels = new ArrayList<ClienteCampoDTO>();
        return "RegistroClientes";
    }
    public void registrarCliente(){
        if(labels != null){
            clienteDTO.setClienteCampoDTOs(labels);
        }
        try {
            if(habEdicion){
                clienteBO.updateCliente(clienteDTO);
                msg=new FacesMessage(FacesMessage.SEVERITY_INFO,"Actualización de cliente exitoso", "Cliente registrado correctamente");
                dispMessage();
            } else {
                clienteDTO = clienteBO.addCliente(clienteDTO);
                msg=new FacesMessage(FacesMessage.SEVERITY_INFO,"Registro de cliente exitoso", "Cliente registrado correctamente");
                dispMessage();
            }
        } catch (InvoiceException ex) {
          Logger.getLogger(ClienteBean.class.getName()).log(Level.SEVERE, null, ex);
          msg=new FacesMessage(FacesMessage.SEVERITY_FATAL, "Error en registro", ex.getMessage());
        }
    
      }
    
   public List<ClienteDTO> listCliente(){
        List<ClienteDTO> list = new ArrayList<ClienteDTO>();
        try{
            list = clienteBO.findClienteAll();
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return list;
    }
    
    public void eliminarCliente(){
        try {
            clienteDTO.setActivo(!ClienteBean.ACTIVO);
            clienteBO.updateCliente(clienteDTO);
        } catch (InvoiceException ex) {
            Logger.getLogger(ClienteBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String regresar(){
     return "ListaClientes";
    }
   
    public String editCliente(){
        habEdicion = true;
        labels = clienteDTO.getClienteCampoDTOs();
        if(labels == null){
            labels = new ArrayList<ClienteCampoDTO>();
        }
        return "RegistroClientes";
    }
    public void cancelarEdicion(){
        habControles=false;
    }
    public ClienteDTO getClienteDTO() {
        return clienteDTO;
    }

    public void setClienteDTO(ClienteDTO clienteDTO) {
        this.clienteDTO = clienteDTO;
    }

    public boolean isHabBusqueda() {
        return habBusqueda;
    }

    public void setHabBusqueda(boolean habBusqueda) {
        this.habBusqueda = habBusqueda;
    }

    public boolean isHabControles() {
        return habControles;
    }

    public void setHabControles(boolean habControles) {
        this.habControles = habControles;
    }
       public boolean getHabEdicion(){
        return habEdicion;
    }
    public void addClienteCampo() {
        ClienteCampoDTO labelsClienteDTO = new ClienteCampoDTO();
        labels.add(labelsClienteDTO);       
    }
    public List<ClienteDTO> completeCliente(String query) {
        query = query.toLowerCase();
        List<ClienteDTO> filteredThemes = new ArrayList<ClienteDTO>();
        try {
            List<ClienteDTO> allThemes = clienteBO.findClienteAll();
            for (ClienteDTO skin : allThemes) {
                if(skin.getIdClienteDes().toLowerCase().startsWith(query) || skin.getNombre().toLowerCase().startsWith(query)) {
                    filteredThemes.add(skin);
                }
            }
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return filteredThemes;
    }
}
